package com.lingnan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.Active;
import com.lingnan.service.ActiveService;
import com.lingnan.service.TargetService;
import com.lingnan.util.DownLoadQiNiu;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.ActiveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/active")
@Api(value = "活动controller")
public class ActiveController {

    @Autowired
    private ActiveService activeService;
    @Autowired
    private TargetService targetService;

    @Resource
    DownLoadQiNiu downLoadQiNiu;


    @PostMapping("/searchActive")
    @ApiOperation("客户端活动搜索")
    public Result searchActive(@ApiParam("根据活动名模糊查询")@RequestBody ActiveVo activeVo){
        List<Active> actives = null;
        if (!ObjectUtils.isEmpty(activeVo)){
            String name = activeVo.getName();
            actives = activeService.searchActive(name);
        }
        if (!ObjectUtils.isEmpty(actives)) {
            return ResultGenerator.genSuccessResult(200, actives, "活动列表搜索成功");
        }
        return ResultGenerator.genErrorResult(201,"活动列表搜索失败");
    }


//    @ApiOperation("文件下载")
//    @PostMapping("/download")
//    public Result download(HttpServletRequest request,@RequestBody ActiveVo activeVo) throws IOException {
//        downLoadQiNiu.download(activeVo.getFile_url());
//       return ResultGenerator.genErrorResult(201,"活动列表搜索失败");
//    }
    /**
     * 客户端活动列表
     * @param
     * @return
     */
    @PostMapping("/activeList")
    @ApiOperation("客户端活动列表")
    public Result activeList(){
        QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("competition_type", "校内", "校外", "非比赛")
                .eq("info_type", "非通知")
                .orderByDesc("begin_time", "end_time");
        List<Active>  list = activeService.list(queryWrapper);
        if (!ObjectUtils.isEmpty(list)) {
            return ResultGenerator.genSuccessResult(200, list, "活动列表展示成功");
        }
        return ResultGenerator.genErrorResult(201,"活动列表展示失败");
    }
    /**
     * 活动详情展示
     * @return
     */
    @PostMapping("/activeDetail")
    @ApiOperation("活动详情展示")
    public Result activeDetail(@ApiParam(value = "传递活动id") @RequestBody ActiveVo activeVo){
        List<Active> list = null;
        if (!ObjectUtils.isEmpty(activeVo)) {
            QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", activeVo.getId());
            list = activeService.list(queryWrapper);
        }
        if (!ObjectUtils.isEmpty(list)) {
            return ResultGenerator.genSuccessResult(200, list, "活动详情展示成功");
        }
        return ResultGenerator.genErrorResult(201,"活动详情展示失败，或者没有该活动");
    }

    /**
     * 文件下载接口 文件从oss对象存储下载
     */
    @ApiOperation("文件下载接口")
    @PostMapping("/fileDownLoad")
    public Result fileDownLoad(@ApiParam(value = "传活动Id") @RequestParam Integer id){
        if (!ObjectUtils.isEmpty(id)){
            // activeService.fileDownLoad(id);
            return ResultGenerator.genSuccessResult(200,"下载成功！");
        }
        return ResultGenerator.genErrorResult(201,"下载失败！");
    }



    /**
     * 已发布通知列表
     * @param
     * @return
     */
    @PostMapping("/noticeList")
    @ApiOperation("已发布通知列表")
    public Result noticeList(){
        QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("competition_type", "通知")
                    .orderByDesc("begin_time", "end_time");
        List<Active> list = activeService.list(queryWrapper);
        if (!ObjectUtils.isEmpty(list)) {
            return ResultGenerator.genSuccessResult(200, list, "已发布通知列表展示成功");
        }
        return ResultGenerator.genErrorResult(201,"已发布通知列表展示失败");
    }
    /**
     * 通知详情展示
     * @return
     */
    @PostMapping("/noticeDetail")
    @ApiOperation("通知详情展示")
    public Result noticeDetail(@ApiParam(value = "传活动id")@RequestBody ActiveVo activeVo){
        List<Active> list = null;
        if (!ObjectUtils.isEmpty(activeVo)) {
            QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", activeVo.getId());
            list = activeService.list(queryWrapper);
        }
        if (!ObjectUtils.isEmpty(list)) {
            return ResultGenerator.genSuccessResult(200, list, "通知详情展示成功");
        }
        return ResultGenerator.genErrorResult(201,"通知详情展示失败，或者没有该通知");
    }

}

